package cn.itcast.web.action;

import cn.itcast.domain.HeiMaClass;
import cn.itcast.serivce.ClassSerivce;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@SuppressWarnings("serial")
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class HeiMaClassAction extends ActionSupport implements ModelDriven<HeiMaClass>{
	//模型驱动
	private HeiMaClass heiMaClass = new HeiMaClass();
	
	@Override
	public HeiMaClass getModel() {
		 
		return heiMaClass;
	}
	
	@Autowired
	private ClassSerivce classSerivce;
	
	
	
	//添加班级
	@Action(value="class_add",results={@Result(name="success",type="redirect",location="../1.html")})
      public String addClass(){
		classSerivce.save(heiMaClass);
    	  return SUCCESS;
      }


	//查询班级
	@Action(value="class_findAll",results={@Result(name="success",type="json")})
	public String findAll(){
		String status = "1";
		 List<HeiMaClass> classFindAll = classSerivce.findByStatus(status);
		 ActionContext.getContext().getValueStack().push(classFindAll);
		  return SUCCESS;
	}
	
	
	
	//修改班级名字
	@Action(value="class_update",results={@Result(name = "success",type = "json")})
	public String update(Integer id,String name){
		classSerivce.update(id,name);
		Map<String,Object > result = new HashMap<>();
		result.put("msg",100); // 100:修改成功
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
	
	
	
	
	//删除班级  将classStatus修改  为1 
	@Action(value="class_delete",results={@Result(name="success",type = "json")})
	public String  updateStatus(String classStatus){
		classSerivce.updateStatus(classStatus);
		return SUCCESS;
	}
	
}
